package com.matrix.index.ui.home.fragment

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import androidx.core.app.ActivityOptionsCompat
import androidx.core.view.size
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseFragment
import com.matrix.index.databinding.FragmentIndexBinding
import com.matrix.index.ui.home.SearchActivity
import kotlinx.coroutines.runBlocking


class IndexFragment : BaseFragment<FragmentIndexBinding>() {

    private var fragments: MutableList<Fragment> = mutableListOf()

    override fun getViewBinding(): FragmentIndexBinding {
        return FragmentIndexBinding.inflate(layoutInflater)
    }


    override fun initView(inflater: LayoutInflater) {
        mViewBinding.tabLayout.addTab(mViewBinding.tabLayout.newTab().setText("推荐"))
        mViewBinding.tabLayout.addTab(mViewBinding.tabLayout.newTab().setText("笑话"))
        mViewBinding.tabLayout.addTab(mViewBinding.tabLayout.newTab().setText("一图"))
        initFragment()
        mViewBinding.viewpager2.adapter = VP2Adapter(fragments, this)
    }

    private fun initFragment() {
        fragments.add(IndexHomeFragment(this))
        fragments.add(IndexJokeFragment())
        fragments.add(IndexBingFragment())
    }

    override fun initData() {

    }

    override fun initListener() {
        mViewBinding.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mViewBinding.tabLayout.getTabAt(position)!!.select()
            }
        })

        mViewBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                mViewBinding.viewpager2.currentItem = tab!!.position
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        mViewBinding.ivSearch.setOnClickListener {
            let{
                val bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    activity as Activity,
                    mViewBinding.ivSearch,
                    "share_image"
                ).toBundle()
                var intent=Intent(context, SearchActivity::class.java)
                intent.putExtras(bundle!!)
                startActivity(intent,bundle)
//            ActivityRouterUtils.startSearchActivity()
            }
        }
    }

    /**
     * 设置viewpager位置
     */
    fun selectViewpager(position: Int) {
        if (fragments.size >= position) {
            mViewBinding.viewpager2.currentItem = position
            mViewBinding.tabLayout.getTabAt(position)!!.select()
        }
    }

    class VP2Adapter(fragments: MutableList<Fragment>, fragment: Fragment) :
        FragmentStateAdapter(fragment) {
        private var fragments: MutableList<Fragment> = fragments

        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }
    }

}
